package com.ymttest.business.service;

import java.io.File;
import java.io.IOException;

import org.apache.http.client.ClientProtocolException;

import com.ymatou.img.api.parameter.UploadImageBean;
import com.ymatou.img.api.service.UploadImageCall;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.FestivalRateConfig;
import com.ymttest.database.sqlwapper.ExpWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

public class ImgAndFileCallService {
	/**
	 * @param fliename
	 *            上传img文件
	 * @param type
	 *            userlogo 类型-头像
	 * @param String
	 *            suffix _o 原图 _m中等 _s 小图
	 * @throws IOException
	 * @throws ClientProtocolException
	 * @return 图片路径
	 */
	public static String uploadImg(String fliename, String type, String suffix) throws ClientProtocolException, IOException {
		Logger.comment("上传图片:" + fliename + ",类型:" + type);
		UploadImageBean uploadbean = new UploadImageBean();
		UploadImageCall uploadcall = new UploadImageCall();
		// String type = "userlogo";
		String turl = "";
		Logger.comment("setPicType:" + type);
		uploadbean.setPicType(type);
		// String fliename = System.getProperty("user.dir") + File.separator +
		// "resource" + File.separator
		// + "Penguins.jpg";
		uploadcall.setData(uploadbean);
		Logger.comment("添加上传文件:" + fliename);
		uploadcall.addPart("file", fliename);

		uploadcall.callService();
		String url = uploadcall.getJsonObject().getAsJsonObject("Result").get("PicUrl").getAsString();
		if (suffix != null && suffix.length() > 0) {
			url = changeUrl(url, suffix);
		}
		return url;
	}

	/**
	 * @param url
	 *            图片地址
	 * @param String
	 *            suffix _o 原图 _m中等 _s 小图
	 * @return
	 */
	public static String changeUrl(String url, String suffix) {
		return url.substring(0, url.lastIndexOf("_")) + suffix + url.substring(url.lastIndexOf("."), url.length());
	}
}
